ActiveReports 12
Using Parameters in SubReports
ActiveReports 12 > ActiveReports User Guide > Samples and Walkthroughs > Walkthroughs > Section Report Walkthroughs > Parameters > Using Parameters in SubReports

Using parameters in SubReport, you can connect a SubReport to the parent report. By setting the parameter on the field that binds the parent report to SubReport, the parent report passes the data to display in SubReport through a parameter. This walkthrough illustrates the method to link the main report with a SubReport using parameters. 

This walkthrough is split up into the following activities:

Note: This walkthrough uses tables from the NorthWind database. By default, in ActiveReports, the NWind.mdb file is located in the [User Documents folder]\GrapeCity Samples\ActiveReports 12\Data folder.
Caution: SubReports do not render PageHeader and PageFooter sections.

When you complete this walkthrough you get a layout that looks similar to the following:

To add an ActiveReport to the Visual Studio project

  1. Create a new Visual Studio project.
  2. From the Project menu, select Add New Item.
  3. In the Add New Item dialog that appears, select ActiveReports 12 Section Report (code-based) and in the Name field, rename the file as rptParent.
  4. Click the Add button to open a new section report in the designer.
  5. From the Project menu, select Add New Item.
  6. In the Add New Item dialog that appears, select ActiveReports 12 Section Report (code-based) and in the Name field, rename the file as rptChild.
  7. Click the Add button to open a second new section report in the designer.

See Adding an ActiveReport to a Project for information on adding different report layouts.

To connect the parent report (rptParent) to a data source

  1. On the detail section band, click the Data Source Icon.
  2. In the Report Data Source dialog that appears, from the OLE DB tab, create a data source connection. See Bind Reports to a Data Source for further details.
  3. Once the connection string field is populated, in the Query field, enter the following SQL query.
    SQL Query
    Copy Code
    Select * from suppliers order by country
    
  4. Click OK to save the data source and return to the report design surface.

To connect the child report (rptChild) to a data source using parameter

  1. On the detail section band, click the Data Source Icon.
  2. In the Report Data Source dialog that appears, from the OLE DB tab, create a data source connection. See Bind Reports to a Data Source for further details.
  3. Once the connection string field is populated, in the Query field, enter the following parameterized SQL query.
    SQL Query
    Copy Code
    SELECT * FROM products INNER JOIN categories ON products.categoryid = categories.categoryid WHERE Products.SupplierID = <%SupplierID%>
    
  4. Click OK to save the data source and return to the report design surface.

To create a layout for the parent report (rptParent)

  1. On the design surface, right click the PageHeader or PageFooter section and select Delete to remove the PageHeader/Footer pair.
  2. Right click on the design surface of the parent report and insert GroupHeader/Footer section pair.
  3. Click the GroupHeader section to select it and go to the Properties window to set the following properties. 
    Property Name Property Value
    Name ghSuppliers
    DataField Country
  4. From the toolbox, drag a TextBox control onto the groupHeader section and in the Properties window, set the properties as follows:
    Property Name Property Value
    DataField Country
    Name txtCountry
    Text Country
    Location 0, 0
    Font Name:Arial, Size:13pt, Bold:True
  5. Click the Detail section to select it and go to the Properties window to set the CanShrink property to True.
  6. From the Visual Studio toolbox, drag and drop the following controls onto the detail section of the report and in the Properties window, set their properties as given below:

    TextBox1

    Property Name Property Value
    DataField CompanyName
    Name txtCompanyName
    Text Company Name
    Location 0.0625, 0.0625
    Size 2.25, 0.2 in
    BackColor Silver
    Font Bold:True

    TextBox2

    Property Name Property Value
    DataField ContactName
    Name txtContactName
    Text Contact Name
    Location 2.312, 0.0625
    Size 1.708, 0.2 in
    BackColor Silver
    Font Bold:True

    TextBox3

    Property Name Property Value
    DataField Phone
    Name txtPhone
    Text Phone
    Location 4.562, 0.0625
    Size 1.542, 0.2 in
    BackColor Silver
    Font Bold:True

    SubReport

    Property Name Property Value
    Name Subreport1
    ReportName ProductName
    Location 0.0625, 0.312

To create a layout for the child report (rptChild)

  1. On the design surface, right click the PageHeader or PageFooter section and select Delete to remove the PageHeader/Footer pair.
  2. Right click on the design surface of the child report and insert GroupHeader/Footer section pair.
  3. Click the GroupHeader section to select it and go to the Properties window to set the following properties. 
    Property Name Property Value
    Name ghProducts
    DataField CategoryName
  4. From the toolbox, drag and drop a TextBox control onto the groupHeader section and in the Properties window, set the following properties.
    Property Name Property Value
    DataField CategoryName
    Name txtCategoryName
    Text Category Name
    Location 0.0625, 0.0625
    Size 2.042, 0.2 in
    ForeColor Maroon
    Font Bold:True
  5. Click the Detail section to select it and go to the Properties window to set the CanShrink property to True
  6. From the toolbox, drag and drop a TextBox control onto the detail section and in the Properties window, set the following properties.
    Property Name Property Value
    DataField ProductName
    Name txtProductName
    Text Product Name
    Location 0.0625, 0.0625
    Size 1.99, 0.2 in
    ForeColor Red

To connect the child report (rptChild) to the SubReport control in parent report (rptParent)

  1. Double-click the gray area around the parent report (rptParent) to create an event-handling method for the ReportStart event.
  2. Add code like the following to the handler to create a new instance of the child report (rptChild).

    To write the code in Visual Basic

    Visual Basic.NET code. Paste JUST ABOVE the ReportStart event.
    Copy Code
    Dim rpt As rptChild
    
    Visual Basic.NET code. Paste INSIDE the ReportStart event.
    Copy Code
    rpt = New rptChild()
    

    To write the code in C#

    C# code. Paste JUST ABOVE the ReportStart event.
    Copy Code
    private rptChild rpt;
    
    C# code. Paste INSIDE the ReportStart event.
    Copy Code
    rpt = new rptChild();
  3. Double-click the detail section of the parent report (rptParent) to create a detail_Format event.
  4. Add code like the following to the handler to display a report in the SubReport control.

    To write the code in Visual Basic

    Visual Basic.NET code. Paste INSIDE the Format event.
    Copy Code
    Me.SubReport1.Report = rpt
    

    To write the code in C#

    C# code. Paste INSIDE the Format event.
    Copy Code
    this.subReport1.Report = rpt;

To set the ShowParametersUI property of SubReport to False

  1. Click the gray area around the child report (rptChild) and go to the Properties window to set the ShowParameterUI property to False.

To view the report

OR

 

See Also

Concepts